package com.ljx.video.controller;

import com.ljx.video.domain.entity.File;
import com.ljx.video.domain.vo.req.FileUploadReq;
import com.ljx.video.domain.vo.resp.ApiResult;
import com.ljx.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2025-01-17  23:57
 */
@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @PostMapping("/upload/chunk")
    public ApiResult<Void> upload(FileUploadReq req){
        videoService.uploadChunk(req);
        return ApiResult.success();
    }

    @GetMapping("/upload/check")
    public ApiResult<Boolean> check(@RequestParam("fileMd5") String fileMd5){
        return ApiResult.success(videoService.check(fileMd5));
    }

    @PostMapping("/chunk/merge")
    public ApiResult<File> mergeChunk(@RequestBody FileUploadReq req){
        return ApiResult.success(videoService.mergeChunk(req));
    }

    @PostMapping("/convert/{fileId}")
    public ApiResult<Void> convertFile(@PathVariable("fileId") Long fileId){
        videoService.convertFile(fileId);
        return ApiResult.success();
    }

    @CrossOrigin
    @GetMapping("/download/{fileId}/")
    public void download(@PathVariable("fileId") Long fileId,
                         HttpServletRequest request,
                         HttpServletResponse response){
        videoService.download(fileId, null, request, response);
    }

    @CrossOrigin
    @GetMapping("/download/{fileId}/{tsNum}")
    public void download(@PathVariable("fileId") Long fileId,
                         @PathVariable("tsNum") String tsNum,
                         HttpServletRequest request,
                         HttpServletResponse response){
        videoService.download(fileId, tsNum, request, response);
    }

}
